﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Mail;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;


public class class_database
{
    private MySqlConnection Conn;
    private MySqlCommand Cmd;
    private String strConn;
    private String FullPathLogFile = System.Configuration.ConfigurationManager.AppSettings["FullPathLogFile"];
    public class_database()
    {
        strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
    }

    /* Hàm mở kết nối 
     -----------------------------------------------------------------*/
    public bool OpenDB()
    {
        try
        {
            this.Conn = new MySqlConnection(this.strConn);
            if (this.Conn.State == ConnectionState.Open)
            {
                this.Conn.Close();
            }
            this.Conn.Open();
            return true;
        }
        catch (Exception ex)
        {
            Show("khong ket noi duoc DB:" + ex.ToString());
            this.CloseDB();
            return false;
        }

    }

    //Chi de doc du lieu
    public MySqlDataReader GetDataReader(String Sql)
    {
        try
        {
            this.OpenDB();
            this.Cmd = new MySqlCommand(Sql, this.Conn);
            MySqlDataReader dtReader = this.Cmd.ExecuteReader();
            this.CloseDB();
            return dtReader;
        }
        catch (Exception)
        {
            return null;
        }
    }

    public DataSet GetDataSet(String Sql)
    {
        try
        {
            this.OpenDB();
            DataSet ds = new DataSet();
            MySqlDataAdapter dtAdapter = new MySqlDataAdapter();
            this.Cmd = new MySqlCommand();
            this.Cmd.Connection = this.Conn;
            this.Cmd.CommandText = Sql;
            this.Cmd.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = this.Cmd;
            dtAdapter.Fill(ds);
            this.CloseDB();
            this.Cmd.Dispose();
            return ds;
        }
        catch (Exception)
        {
            return null;
        }
    }

    public DataTable GetDataTable(String Sql)
    {
        try
        {
            this.OpenDB();
            DataTable dt = new DataTable();
            MySqlDataAdapter dtAdapter = new MySqlDataAdapter(Sql, this.Conn);
            dtAdapter.SelectCommand.CommandTimeout = 0;
            dtAdapter.Fill(dt);
            this.CloseDB();
            return dt;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public DataTable GetDataTableSetTimeout(String Sql)
    {
        try
        {
            this.OpenDB();
            DataTable dt = new DataTable();
            MySqlDataAdapter dtAdapter = new MySqlDataAdapter(Sql, this.Conn);
            dtAdapter.SelectCommand.CommandTimeout = 0;
            dtAdapter.Fill(dt);
            this.CloseDB();
            return dt;
        }
        catch (Exception)
        {
            return null;
        }
    }


    // Trả về DataTable với tham số parameter (parameter có thể thay đổi dựa theo biến), vì sql là một chuỗi cố định 
    // nên không thể thay đổi được
    public DataTable GetDatatableWithParameters(string Sql, string[] ArrParameter, string[] ArrValue)
    {
        DataTable dt = new DataTable();
        try
        {
            this.OpenDB();
            MySqlCommand Cmd = new MySqlCommand(Sql, Conn);
            for (int i = 0; i < ArrParameter.Length; i++)
            {
                Cmd.Parameters.AddWithValue(ArrParameter[i], ArrValue[i]);
            }
            MySqlDataAdapter da = new MySqlDataAdapter();
            da.SelectCommand = Cmd;
            da.Fill(dt);
            this.CloseDB();
            return dt;
        }
        catch (Exception ex)
        {
            return null;
            //Response.Write(ex.ToString());
        }
    }


    // sử dụng để chèn và xóa dữ liệu (sql: insert, delete)
    public bool QueryExecuteNonQuery(String Sql)
    {
        try
        {
            this.OpenDB();
            this.Cmd = new MySqlCommand();
            this.Cmd.Connection = this.Conn;
            this.Cmd.CommandType = CommandType.Text;
            this.Cmd.CommandText = Sql;
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }


    // trả về một giá trị vô hướng (sql: select count(*) ...)
    public Object QueryExecuteScalar(String Sql)
    {
        try
        {
            this.OpenDB();
            Object obj;
            this.Cmd = new MySqlCommand();
            this.Cmd.Connection = this.Conn;
            this.Cmd.CommandType = CommandType.Text;
            this.Cmd.CommandText = Sql;
            obj = this.Cmd.ExecuteScalar();
            this.CloseDB();
            this.Cmd.Dispose();
            return obj;
        }
        catch (Exception)
        {
            return null;
        }
    }



    public Object QueryExecuteScalarNoConnect(String Sql)
    {
        try
        {
            Object obj;
            this.Cmd = new MySqlCommand();
            this.Cmd.Connection = this.Conn;
            this.Cmd.CommandType = CommandType.Text;
            this.Cmd.CommandText = Sql;
            obj = this.Cmd.ExecuteScalar();
            this.Cmd.Dispose();
            return obj;
        }
        catch (Exception)
        {
            return null;
        }
    }



    //----Ham tra ve chuoi sql----
    public string select(string _table, string _select, string _where = "", string _ordering = "", string _limit = "", string _other = "")
    {
        string _sql = "";
        if (_select == "")
            _select = "*";
        if (_where != "")
            _where = " WHERE " + _where;
        if (_limit != "")
            _limit = " LIMIT " + _limit;
        if (_ordering != "")
            _ordering = " ORDER BY " + _ordering;
        _sql = "SELECT " + _select + " FROM " + _table + _other + _where + _ordering + _limit;
        return _sql;
    }

    //---- Ham dem so ban ghi cua mot ban         
    public int Count(string _table, string _where)
    {
        int count;
        string sql = this.select(_table, "COUNT(*) as tong", _where);
        count = Convert.ToInt32(this.QueryExecuteScalar(sql));
        return count;
    }

    /*Hàm trả về 1 giá trị object (1 giá trị - giá trị vô hướng)
     --------------------------------------*/
    public object GetValue(string _table, string _select, string _where = "")
    {
        try
        {
            //this.OpenDB();
            string sql = this.select(_table, _select, _where);
            object obj = this.QueryExecuteScalar(sql);
            if (obj is DBNull || obj == null)
                obj = "";
            else
                obj = obj.ToString();
            //this.CloseDB();
            return obj;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return null;
        }
    }



    public object GetValueNoConnect(string _table, string _select, string _where = "")
    {
        try
        {
            string sql = this.select(_table, _select, _where);
            object obj = this.QueryExecuteScalarNoConnect(sql);
            if (obj is DBNull || obj == null)
                obj = "";
            else
                obj = obj.ToString();
            return obj;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return null;
        }
    }
    /*Hàm trả về 1 giá trị
    --------------------------------------*/
    public object GetMaxValue(string _table, string _select, string _where = "")
    {
        try
        {
            this.OpenDB();
            string sql = "SELECT MAX(" + _select + ") AS " + _select + " FROM " + _table;
            object obj = this.QueryExecuteScalar(sql);
            if (obj is DBNull || obj == null)
                obj = "";
            else
                obj = obj.ToString();
            this.CloseDB();
            return obj;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public string GetNextID(string _table, string _select, string _where = "")
    {
        string Output = "1";
        try
        {
            this.OpenDB();
            string sql = "SELECT MAX(" + _select + ") AS " + _select + " FROM " + _table;
            object obj = this.QueryExecuteScalar(sql);
            if (obj != null)
            {
                Output = (Convert.ToInt32(obj) + 1).ToString();
            }
            this.CloseDB();
            return Output;
        }
        catch (Exception)
        {
            return Output;
        }
    }

    /* InsertRecord() - Thêm bản ghi mới
    ------------------------------------------------------------------*/
    public bool InsertRecord(string _table, string[] _param, string[] _value, string[] _type)
    {
        try
        {
            this.OpenDB();
            string _nameParam = string.Empty;
            string _valueParam = string.Empty;

            for (int i = 0; i < _param.Length; i++)
            {
                _nameParam += _param[i];
                _valueParam += "@" + @_param[i];
                if (i < _param.Length - 1)
                {
                    _nameParam += ","; // Bo dau phay cuoi cung                    
                    _valueParam += ",";
                }
            }
            string CommandText = "INSERT INTO " + _table + " (" + _nameParam + ") VALUES (" + _valueParam + ")";
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            for (int i = 0; i < _param.Length; i++)
            {
                //_param[i] = "@" + _param[i];         
                switch (_type[i])
                {
                    case "string":
                        this.Cmd.Parameters.AddWithValue(_param[i], _value[i]);
                        break;
                    case "int":
                        this.Cmd.Parameters.AddWithValue(_param[i], Int32.Parse(_value[i]));
                        break;
                    case "date":
                        this.Cmd.Parameters.AddWithValue(_param[i], DateTime.Parse(_value[i]));
                        break;
                }


            }
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return false;
        }
    }


    public bool InsertRecordNoConnect(string _table, string[] _param, string[] _value, string[] _type)
    {
        try
        {
            string _nameParam = string.Empty;
            string _valueParam = string.Empty;

            for (int i = 0; i < _param.Length; i++)
            {
                _nameParam += _param[i];
                _valueParam += "@" + @_param[i];
                if (i < _param.Length - 1)
                {
                    _nameParam += ","; // Bo dau phay cuoi cung                    
                    _valueParam += ",";
                }
            }
            string CommandText = "INSERT INTO " + _table + " (" + _nameParam + ") VALUES (" + _valueParam + ")";
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            for (int i = 0; i < _param.Length; i++)
            {
                //_param[i] = "@" + _param[i];         
                switch (_type[i])
                {
                    case "string":
                        this.Cmd.Parameters.AddWithValue(_param[i], _value[i]);
                        break;
                    case "int":
                        this.Cmd.Parameters.AddWithValue(_param[i], Int32.Parse(_value[i]));
                        break;
                    case "date":
                        this.Cmd.Parameters.AddWithValue(_param[i], DateTime.Parse(_value[i]));
                        break;
                }


            }
            this.Cmd.ExecuteNonQuery();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return false;
        }
    }


    /* UpdateRecord() - Cap nhat bản ghi
    ------------------------------------------------------------------*/
    public bool UpdateRecord(string _table, string[] _param, string[] _value, string _where)
    {
        try
        {
            this.OpenDB();
            string _nameParam = string.Empty;
            string _valueParam = string.Empty;
            string _input = "";
            for (int i = 0; i < _param.Length; i++)
            {
                _input += _param[i] + "=@" + _param[i];
                if (i < _param.Length - 1)
                {
                    _input += ",";//Xoa dau phay cuoi cung
                }
            }
            string CommandText = "UPDATE " + _table + " SET " + _input + " WHERE " + _where;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            for (int i = 0; i < _param.Length; i++)
            {
                _param[i] = "@" + _param[i];
                if (class_string.IsNumberFloat(_value[i]) == false)
                {
                    this.Cmd.Parameters.Add(_param[i], MySqlDbType.VarChar, 255);
                    this.Cmd.Parameters[_param[i]].Value = _value[i];
                }
                else
                {
                    this.Cmd.Parameters.AddWithValue(_param[i], _value[i]);
                }
            }
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return false;
        }
    }


    /* ------------------------------------------------------------------*/
    public bool UpdateRecordNoConnect(string _table, string[] _param, string[] _value, string _where)
    {
        try
        {
            string _nameParam = string.Empty;
            string _valueParam = string.Empty;
            string _input = "";
            for (int i = 0; i < _param.Length; i++)
            {
                _input += _param[i] + "=@" + _param[i];
                if (i < _param.Length - 1)
                {
                    _input += ",";//Xoa dau phay cuoi cung
                }
            }
            string CommandText = "UPDATE " + _table + " SET " + _input + " WHERE " + _where;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            for (int i = 0; i < _param.Length; i++)
            {
                _param[i] = "@" + _param[i];
                if (class_string.IsNumberFloat(_value[i]) == false)
                {
                    this.Cmd.Parameters.Add(_param[i], MySqlDbType.VarChar, 255);
                    this.Cmd.Parameters[_param[i]].Value = _value[i];
                }
                else
                {
                    this.Cmd.Parameters.AddWithValue(_param[i], _value[i]);
                }
            }
            this.Cmd.ExecuteNonQuery();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return false;
        }
    }

    /* DeleteRecord() - Xoa bản ghi
    ------------------------------------------------------------------*/
    public bool DeleteRecord(string _table, string _where)
    { 
        // _where Id= 1; 
        try
        {
            this.OpenDB();
            string CommandText = "DELETE FROM " + _table + " WHERE " + _where;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            Show("Error" + ex.ToString());
            return false;
        }
    }

    /* Hàm đóng kết nối
     ------------------------------------------------------------------*/
    public void CloseDB()
    {
        if (this.Conn.State == ConnectionState.Open)
        {
            this.Conn.Close();
            this.Conn.Dispose();
        }
    }
    /*-----------KhoiNx*/

    // Trả lại DataTable có trường số thứ thự
    public DataTable GetDataTableAddColumnStt(String Sql)
    {
        DataTable dt = new DataTable();
        dt = GetDataTable(Sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = "Stt";
            dc.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(dc);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Stt"] = (i + 1).ToString();
                //string date = ConvertTime(dt.Rows[i]["SDate"].ToString());
                //dt.Rows[i]["SDate"] = date;
            }
        }
        return dt;
    }
    public DataTable GetDataTableAddColumnStt1(String Sql)
    {
        DataTable dt = new DataTable();
        dt = GetDataTable(Sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = "Stt";
            dc.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(dc);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Stt"] = (i + 1).ToString();

            }
        }
        return dt;
    }
    public DataTable GetDataTableAddColumnSttSetTimeout(String Sql)
    {
        DataTable dt = new DataTable();
        dt = GetDataTableSetTimeout(Sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            DataColumn dc = new DataColumn();
            dc.ColumnName = "Stt";
            dc.DataType = System.Type.GetType("System.String");
            dt.Columns.Add(dc);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Stt"] = (i + 1).ToString();
            }
        }
        return dt;
    }

    // 
    public string CheckSumMd5String(string str)
    {
        MD5 md5 = MD5.Create();
        return BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(str))).Replace("-", string.Empty);
    }
    public DataTable GetTopDatatable(DataTable dt, int StartRow, int CountRows)
    {
        DataTable _dt = new DataTable();
        int Count = dt.Rows.Count;
        if (Count > 0)
        {
            _dt = dt.Clone();
            for (int i = StartRow; i < StartRow + CountRows; i++)
            {
                if (i >= Count)
                    break;
                else
                    _dt.ImportRow(dt.Rows[i]);
            }
        }
        return dt;
    }
    public DataTable GetDatatableFromDbOrCache(string Sql, string CacheName, int CacheMinute = 15)
    {
        DataTable dt = new DataTable();
        if (HttpContext.Current.Cache.Get(CacheName) == null)
        {
            dt = GetDataTable(Sql);
            HttpContext.Current.Cache.Insert(CacheName, dt, null, DateTime.MaxValue, TimeSpan.FromMinutes(CacheMinute));
        }
        else
        {
            dt = (DataTable)(HttpContext.Current.Cache.Get(CacheName));
        }

        return dt;
    }
    public DataTable GetDatatableFromCache(string CacheName)
    {
        DataTable dt = new DataTable();
        if (HttpContext.Current.Cache.Get(CacheName) != null)
            dt = (DataTable)(HttpContext.Current.Cache.Get(CacheName));
        return dt;
    }

    public void InsertDatatableToCache(string Sql, string CacheName)
    {
        if (HttpContext.Current.Cache.Get(CacheName) == null)
            HttpContext.Current.Cache.Insert(CacheName, GetDataTable(Sql), null, DateTime.MaxValue, TimeSpan.FromDays(30));
    }
    public void UpdateColumnTblscanrequest(string ActiveCode)
    {
        if (ActiveCode != " ")
            ActiveCode = ActiveCode + "_";
        string Sql = "ALTER TABLE `" + ActiveCode + "tblscanrequest`";
        Sql += " ADD COLUMN `ScanType`  int(10) NULL AFTER `TimeSend`,";
        Sql += " ADD COLUMN `MonthOfTime`  int(10) NULL AFTER `ScanType`,";
        Sql += " ADD COLUMN `YearOfTime`  int(10) NULL AFTER `MonthOfTime`,";
        Sql += " ADD COLUMN `TimeDayOfWeek`  int(10) NULL AFTER `YearOfTime`,";
        Sql += " ADD COLUMN `TimeOfHour`  int(10) NULL AFTER `TimeDayOfWeek`,";
        Sql += " ADD COLUMN `TimeOfMinute`  int(10) NULL AFTER `TimeOfHour`,";
        Sql += " ADD COLUMN `TimeOnStartup`  tinyint(1) NULL AFTER `TimeOfMinute`,";
        Sql += " ADD COLUMN `ScanDrive`  tinyint(1) NULL AFTER `TimeOnStartup`,";
        Sql += " ADD COLUMN `ScanOneFile`  tinyint(1) NULL AFTER `ScanDrive`,";
        Sql += " ADD COLUMN `ScanAllFile`  tinyint(1) NULL AFTER `ScanOneFile`,";
        Sql += " ADD COLUMN `RemoveNoAsk`  tinyint(1) NULL AFTER `ScanAllFile`,";
        Sql += " ADD COLUMN `BackupAfRemove`  tinyint(1) NULL AFTER `RemoveNoAsk`;";
        class_database db = new class_database();
        db.QueryExecuteNonQuery(Sql);
    }
    public string SendEmailToUser(string FromMail, string PassWord, string ReviceMail, string Subject, string Body)
    {
        try
        {
            //// caspytrading@gmail.com
            //// toantin2k48  
            MailMessage mM = new MailMessage();
            mM.From = new MailAddress(FromMail);
            mM.To.Add(ReviceMail);
            mM.Subject = Subject;
            //mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
            mM.Body = Body;
            mM.IsBodyHtml = true;
            SmtpClient sC = new SmtpClient("smtp.gmail.com");
            sC.Port = 587;
            sC.Credentials = new NetworkCredential(FromMail, PassWord);
            sC.EnableSsl = true;
            sC.Send(mM);
            return "";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
    public string GetChildServerActiveCode()
    {
        string ChildServerActiveCode = HttpContext.Current.Request.QueryString["ChildServerActiveCode"];
        if (ChildServerActiveCode != null)
        {
            ChildServerActiveCode = ChildServerActiveCode.Trim();
            ChildServerActiveCode = ChildServerActiveCode.Replace("_", "");
        }
        string Sql = "select ActiveCode from tblsubservers where ActiveCode = @ActiveCode ";
        DataTable dt = new DataTable();
        string[] ArrParameters = new[] { "@ActiveCode" };
        string[] ArrValues = new[] { ChildServerActiveCode };
        dt = GetDatatableWithParameters(Sql, ArrParameters, ArrValues);
        if (dt != null && dt.Rows.Count > 0)
            return ChildServerActiveCode + "_";
        else
            return " ";
    }
    public void CacherDataTableOnMem(string Sql, string CacheName)
    {
        HttpContext.Current.Cache.Insert(CacheName, GetDataTable(Sql), null, DateTime.MaxValue, TimeSpan.FromDays(30));

    }
    public bool deletel_data(string Table)
    {
        try
        {
            this.OpenDB();
            string CommandText = "DELETE FROM " + Table;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }

    }

    public bool InsertData_Talbe(string TableName, string[] ArrParamecter, string[] ArrValue, string[] ArrType)
    {
        MySqlConnection Conn = new MySqlConnection(strConn);
        CultureInfo enUS = new CultureInfo("en-US");
        if (Conn.State == ConnectionState.Open)
        {
            Conn.Close();
        }
        try
        {
            Conn.Open();
            string _nameParam = string.Empty;
            string _valueParam = string.Empty;
            for (int i = 0; i < ArrParamecter.Length; i++)
            {
                _nameParam += ArrParamecter[i];
                _valueParam += "@" + ArrParamecter[i];
                if (i < ArrParamecter.Length - 1)
                {
                    _nameParam += ","; // Bo dau phay cuoi cung                    
                    _valueParam += ",";
                }
            }
            string CommandText = "INSERT " + TableName + " (" + _nameParam + ") VALUES (" + _valueParam + ")";
            this.Cmd = new MySqlCommand(CommandText, Conn);
            for (int i = 0; i < ArrParamecter.Length; i++)
            {

                switch (ArrType[i])
                {
                    case "string":
                        this.Cmd.Parameters.AddWithValue(ArrParamecter[i], ArrValue[i]);
                        break;
                    case "int":
                        this.Cmd.Parameters.AddWithValue(ArrParamecter[i], Int32.Parse(ArrValue[i]));
                        break;
                    case "date":
                        this.Cmd.Parameters.AddWithValue(ArrParamecter[i], DateTime.Parse(ArrValue[i]));
                        break;
                }
            }
            this.Cmd.ExecuteNonQuery();
            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
        }
    }
    public bool Creat_Table()
    {
        string sql = "CREATE TABLE aaaaa123( ManualLogID int(10) ,ClientID int(10), ScanPath VARCHAR(255) )";
        try
        {
            this.OpenDB();
            string CommandText = sql;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }


    }
    public string ConvertTime(string input) // 20120513 ---> 13-05-2015
    {
        string a = input.Substring(0, 4);
        string b = input.Substring(4, 2);
        string c = input.Substring(6, 2);
        input = c + "-" + b + "-" + a;
        return input;
    }
    public bool Creat_Table1()
    {
        string sql = "CREATE TABLE Temp( ClientID int(10) ,sAddress VARCHAR(255), nReceivedMsg int(10), nAddrType VARCHAR(255) )";
        try
        {
            this.OpenDB();
            string CommandText = sql;
            this.Cmd = new MySqlCommand(CommandText, this.Conn);
            this.Cmd.ExecuteNonQuery();
            this.CloseDB();
            this.Cmd.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }


    }
    public static void Show(string message)
    {
        string cleanMessage = message.Replace("'", "\'");
        Page page = HttpContext.Current.CurrentHandler as Page;
        string script = string.Format("alert('{0}');", cleanMessage);
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", script, true);
        }
    }
    public void WriteLog(string TextInput)
    {
        try
        {
            bool createdNew;
            using (Mutex filelog = new Mutex(false, "WriteLog", out createdNew))
            {
                if (createdNew)
                {
                    filelog.WaitOne(1000000000);
                    FileInfo fi = new FileInfo(FullPathLogFile);
                    if ((fi.Exists))
                    {
                        if (fi.Length > 2 * 1024 * 1024)
                        {
                            List<string> lines = new List<string>(File.ReadAllLines(FullPathLogFile));
                            lines.RemoveAt(0);
                            while (lines.Count > 0)
                            {
                                lines.RemoveAt(0);
                            }
                            lines.Add(TextInput);
                            File.WriteAllLines(FullPathLogFile, lines.ToArray());
                        }
                        else
                        {
                            using (StreamWriter Sw = File.AppendText(FullPathLogFile))
                            {
                                Sw.WriteLine(DateTime.Now.ToString() + "   " + TextInput);
                                Sw.Close();
                            }
                        }
                    }
                    Thread.Sleep(10);
                    filelog.ReleaseMutex();
                }
            }
        }
        catch (Exception)
        {

        }

    }

}
